package one.bugu.android.demon.ui.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationManagerCompat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.common.util.StringUtil;
import com.juefeng.android.framework.common.util.ToastUtils;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.CoinMarkBean;
import one.bugu.android.demon.bean.SetWarnBean;
import one.bugu.android.demon.bean.UserSmsInfoBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.BigDecimalUtil;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

/**
 * 设置预警界面
 */
@LKContentView(R.layout.activity_setwarn)
public class SetWarnActivity extends MyBaseActivity {

    @LKInjectView(R.id.btbc_set_warn)
    private BaseTopBarView btbvSetWarn;

    /**
     * 自定义选项
     */
    @LKInjectView(R.id.set_custom)
    private RelativeLayout setCustom;

    /**
     * 自定义图标
     */
    @LKInjectView(R.id.custom_icon)
    private ImageView customIcon;

    /**
     * 自定义内容
     */
    @LKInjectView(R.id.custom_content)
    private LinearLayout customContent;

    /**
     * 极速设置选项
     */
    @LKInjectView(R.id.set_js)
    private RelativeLayout setJs;

    /**
     * 极速设置图标
     */
    @LKInjectView(R.id.js_icon)
    private ImageView jsIcon;

    /**
     * 极速设置内容
     */
    @LKInjectView(R.id.js_content)
    private LinearLayout jsContent;

    /**
     * 预警方式选项
     */
    @LKInjectView(R.id.set_way)
    private RelativeLayout setWay;

    /**
     * 预警方式图标
     */
    @LKInjectView(R.id.way_icon)
    private ImageView wayIcon;

    /**
     * 预警方式内容
     */
    @LKInjectView(R.id.way_content)
    private LinearLayout wayContent;

    /**
     * 保存设置按钮
     */
    @LKInjectView(R.id.btn_save)
    private Button btnSave;

    /**
     * 交易对
     */
    @LKInjectView(R.id.setWarn_jyd)
    private TextView setWarnJyd;

    /**
     * 平台
     */
    @LKInjectView(R.id.setWarn_platform)
    private TextView setWarnPlatform;

    /**
     * 币名
     */
    @LKInjectView(R.id.setWarn_coinName)
    private TextView setWarnCoinName;

    /**
     * 当前价格
     */
    @LKInjectView(R.id.setWarn_price)
    private TextView setWarnPrice;

    /**
     * 涨幅
     */
    @LKInjectView(R.id.setWarn_increase)
    private TextView setWarnIncrease;

    /**
     * 涨幅图片
     */
    @LKInjectView(R.id.setWarn_increaseIcon)
    private LinearLayout setWarnIncreaseIcon;

    /**
     * 上涨百分比编辑框
     */
    @LKInjectView(R.id.percentage_rise)
    private EditText percentageRise;

    /**
     * 上涨价格编辑框
     */
    @LKInjectView(R.id.price_rise)
    private EditText priceRise;

    /**
     * 交易对的relativeTrade(斜杠后面名字)
     */
    @LKInjectView(R.id.set_nameIncreate)
    private TextView setNameIncreate;

    /**
     * 下跌百分比编辑框
     */
    @LKInjectView(R.id.percentage_fall)
    private EditText percentageFall;

    /**
     * 下跌价格编辑框
     */
    @LKInjectView(R.id.price_fall)
    private EditText priceFall;

    /**
     * 交易对的relativeTrade(斜杠后面名字)
     */
    @LKInjectView(R.id.set_nameFall)
    private TextView setNameFall;

    /**
     * 盘中大涨/大跌按钮
     */
    @LKInjectView(R.id.btn_pan)
    private Switch btnPan;

    /**
     * 极速拉升/猛烈打压按钮
     */
    @LKInjectView(R.id.btn_js)
    private Switch btnJs;

    /**
     * 极速反弹/高台跳水按钮
     */
    @LKInjectView(R.id.btn_ft)
    private Switch btnFt;

    /**
     * 站内提醒按钮
     */
    @LKInjectView(R.id.btn_station)
    private Switch btnStation;

    /**
     * 微信提醒按钮
     */
    @LKInjectView(R.id.btn_weiXin)
    private Switch btnWeiXin;

    /**
     * 语音提醒按钮
     */
    @LKInjectView(R.id.btn_tel)
    private Switch btnTel;

    /**
     * 短信提醒按钮
     */
    @LKInjectView(R.id.btn_shortMessage)
    private Switch btnShortMessage;

    /**
     * 涨幅为0时显示的文字
     */
    @LKInjectView(R.id.increate_zero)
    private TextView increateZero;

    /**
     * 获取焦点的地方
     */
    @LKInjectView(R.id.txt_lostFocus)
    private LinearLayout txtLostFocus;

    /**
     * 获取焦点的地方
     */
    @LKInjectView(R.id.re_lostFocus)
    private RelativeLayout reLostFocus;

    private String platformName;
    private String coinName;
    private String relativeTrade;
    private String platformId;
    private String coinId;
    private String coinNameCN;

    String token;

    private double price;


    private Context context;

    private boolean isOpen = true;

    private DecimalFormat decimalFormat = new DecimalFormat("0.00");
    private DecimalFormat df = new DecimalFormat("#.########");

    @Override
    protected void initView() {
        super.initView();
        //设置状态栏
        StatusBarUtils.getInstance().setStatusBar("#FFFFFF", this, btbvSetWarn, false);

        platformName = getIntent().getStringExtra("platformName");
        relativeTrade = getIntent().getStringExtra("relativeTrade");
        coinName = getIntent().getStringExtra("coinName");
        platformId = getIntent().getStringExtra("platformId");
        coinId = getIntent().getStringExtra("coinId");
        coinNameCN = getIntent().getStringExtra("coinNameCN");

        //设置交易对 平台 涨幅等信息
        setWarnJyd.setText(coinName + "/" + relativeTrade);
        setWarnPlatform.setText(platformName);
        setWarnCoinName.setText(coinNameCN);
        setNameIncreate.setText(relativeTrade);
        setNameFall.setText(relativeTrade);

        token = PreferencesUtil.getInstance().getString(SetWarnActivity.this, Constant.TOKEN);

        //所有按钮默认关闭
        btnPan.setChecked(false);
        btnJs.setChecked(false);
        btnFt.setChecked(false);
        btnStation.setChecked(false);
        btnShortMessage.setChecked(false);
        btnWeiXin.setChecked(false);
        btnTel.setChecked(false);

        //极速设置内容默认隐藏
//        jsContent.setVisibility(View.GONE);
        jsIcon.setImageResource(R.mipmap.jiantouxia);

        getCoinMarket();
        getWarnInfo();
    }

    /**
     * 获取最新价格
     */
    public void getCoinMarket() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("platformId", platformId);
        map.put("coinId", coinId);
        map.put("relativeTrade", relativeTrade);
        LKUtil.getHttpManager().postBody(HttpConstant.GETNEWPRICE_URL, map, getCoinMarketHanlder);
    }

    /**
     * 获取最新价格的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<CoinMarkBean> getCoinMarketHanlder = new BaseHttpAsycResponceHandler<CoinMarkBean>() {

        @SuppressLint("SetTextI18n")
        @Override
        public void onSuccess(CoinMarkBean data) {
            super.onSuccess(data);

            BigDecimal bigDecimal = new BigDecimal(data.getData().get("price"));
            //科学计数法转换 设置价格
            setWarnPrice.setText(bigDecimal.toPlainString());
            //根据涨幅设置显示的图片  为0时显示0.00%
            if (StringUtil.isEmpty(data.getData().get("priceUpRatio"))) {
                setWarnIncrease.setBackgroundResource(R.drawable.shape_warn_up);
            } else {
                double ratio = Double.parseDouble(data.getData().get("priceUpRatio"));

                if (ratio > 0) {
                    //涨幅大于0 展示上涨的图片
                    setWarnIncrease.setText("+" + decimalFormat.format(ratio * 100) + "%");
                    setWarnIncrease.setBackgroundResource(R.drawable.shape_warn_up);

                    setWarnIncreaseIcon.setVisibility(View.VISIBLE);
                    increateZero.setVisibility(View.GONE);

                } else if (ratio < 0) {
                    //涨幅小于0 展示下跌的图片
                    setWarnIncrease.setText(decimalFormat.format(ratio * 100) + "%");
                    setWarnIncrease.setBackgroundResource(R.drawable.shape_warn_down);
                    setWarnIncreaseIcon.setVisibility(View.VISIBLE);
                    increateZero.setVisibility(View.GONE);

                } else {
                    //涨幅等于0 显示文字0.00%
                    String relativeTrade = PreferencesUtil.getInstance().getString(SetWarnActivity.this, "relativeTrade");
                    String priceUsdt = PreferencesUtil.getInstance().getString(SetWarnActivity.this, "priceUsdt");
                    String tempKey  = data.getData().get("platformId") + "_" + data.getData().get("coinId") + "_" + relativeTrade;
                    Double openPrice = data.getOpenPrice().get(tempKey);
                    if (openPrice == 0 || openPrice == null){
                        setWarnIncreaseIcon.setVisibility(View.GONE);
                        increateZero.setVisibility(View.VISIBLE);
                    }else {
                        Double num1 = Double.parseDouble(priceUsdt);
                        Double num2 = openPrice;
                        Double num = (num1 - num2) / num2;
                        if (num > 0) {
                            setWarnIncrease.setText("+" + decimalFormat.format(num * 100) + "%");
                            setWarnIncrease.setBackgroundResource(R.drawable.shape_warn_up);
                            setWarnIncreaseIcon.setVisibility(View.VISIBLE);
                            increateZero.setVisibility(View.GONE);
                        } else if (num < 0){
                            setWarnIncrease.setText(decimalFormat.format(num * 100) + "%");
                            setWarnIncrease.setBackgroundResource(R.drawable.shape_warn_down);
                            setWarnIncreaseIcon.setVisibility(View.VISIBLE);
                            increateZero.setVisibility(View.GONE);
                        }else {
                            setWarnIncreaseIcon.setVisibility(View.GONE);
                            increateZero.setVisibility(View.VISIBLE);

                        }
                    }
                }
            }
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> setSZHanlder = new BaseHttpAsycResponceHandler<String>() {

        @SuppressLint("SetTextI18n")
        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);
            Double price = Double.parseDouble(data);
            Double num = ((Double.parseDouble(priceRise.getText().toString()) - price) / price) * 100;
            percentageRise.setText(BigDecimalUtil.formatMoney(num));
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    /**
     * 计算输入数值的方法
     * @param type
     * @param value
     */
    public void setXD(int type, Double value) {

        BigDecimal bigDecimal = new BigDecimal(price);

        if (type == 1) {
            priceRise.setText(df.format(Double.parseDouble(setWarnPrice.getText().toString()) * (1 + value / 100)) + "");
        }
        if (type == 3) {
            priceFall.setText(df.format(Double.parseDouble(setWarnPrice.getText().toString()) * (1 - value / 100)) + "");
        }
        if (type == 2) {
            percentageRise.setText(df.format((value - Double.parseDouble(setWarnPrice.getText().toString())) / Double.parseDouble(setWarnPrice.getText().toString()) * 100) + "");
        }
        if (type == 4) {
            percentageFall.setText(df.format(Math.abs((value - Double.parseDouble(setWarnPrice.getText().toString()))) / Double.parseDouble(setWarnPrice.getText().toString()) * 100) + "");
        }
    }

    /**
     * 获取设置预警信息
     */
    public void getWarnInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("platformId", platformId);
        map.put("coinId", coinId);
        map.put("relativeTrade", relativeTrade);
        LKUtil.getHttpManager().postBody(HttpConstant.GETPRICE_URL, map, getWarnInfoHanlder);
    }

    /**
     * 获取预警信息的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<List<SetWarnBean>> getWarnInfoHanlder = new BaseHttpAsycResponceHandler<List<SetWarnBean>>() {

        @Override
        public void onSuccess(List<SetWarnBean> setWarnBeans) {
            super.onSuccess(setWarnBeans);

            percentageRise.setText(null);
            percentageFall.setText(null);

            //根据返回数据 设置按钮状态
            if (setWarnBeans != null) {
                for (int i = 0; i < setWarnBeans.size(); i++) {
                    if (setWarnBeans.get(i).getConfigTypeName().equals("上涨")) {
                        if (setWarnBeans.get(i).getConfigValue() != null) {
                            priceRise.setText(df.format(Double.parseDouble(setWarnBeans.get(i).getConfigValue())));
                        }
                    }
                    if (setWarnBeans.get(i).getConfigTypeName().equals("下跌")) {
                        if (setWarnBeans.get(i).getConfigValue() != null) {
                            priceFall.setText(df.format(Double.parseDouble(setWarnBeans.get(i).getConfigValue())));
                        }
                    }

                    if (setWarnBeans.get(i).getConfigTypeName().equals("盘中大涨")) {
                        btnPan.setChecked(true);
                    }

                    if (setWarnBeans.get(i).getConfigTypeName().equals("急速拉升")) {
                        btnJs.setChecked(true);
                    }

                    if (setWarnBeans.get(i).getConfigTypeName().equals("快速反弹")) {
                        btnFt.setChecked(true);
                    }
                }
                if (!setWarnBeans.isEmpty()) {
                    if (setWarnBeans.get(0).getRemindWay().contains("10")) {
                        btnWeiXin.setChecked(true);
                    }
                    if (setWarnBeans.get(0).getRemindWay().contains("20")) {
                        btnShortMessage.setChecked(true);
                    }
                    if (setWarnBeans.get(0).getRemindWay().contains("30")) {
                        btnTel.setChecked(true);
                    }
                    if (setWarnBeans.get(0).getRemindWay().contains("40")) {
                        btnStation.setChecked(true);
                    }
                }
                isOpen = false;
            }

        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
            isOpen = false;
        }

        @Override
        public void onError() {
            super.onError();
            isOpen = false;
        }
    };

    /**
     * 设置预警
     */
    public void setWarnConfig() {

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("platformId", platformId);
        map.put("coinId", coinId);
        map.put("relativeTrade", relativeTrade);
        map.put("openFlag", "0");
        StringBuilder sb = new StringBuilder();

        //根据按钮状态 传入参数
        //微信提醒按钮打开
        if (btnWeiXin.isChecked()) {
            sb.append("10,");
        }
        //短信提醒按钮打开
        if (btnShortMessage.isChecked()) {
            sb.append("20,");
        }
        //语音提醒按钮打开
        if (btnTel.isChecked()) {
            sb.append("30,");
        }
        //站内提醒按钮打开
        if (btnStation.isChecked()) {

            sb.append("40,");
        }

        String remindWay = sb.toString();

        map.put("remindWay", remindWay);

        StringBuilder bs = new StringBuilder();
        StringBuilder configValue = new StringBuilder();

        //上涨价格编辑框不为空且输入值大于当前价格
        if (priceRise.getText().toString().length() != 0 && Double.parseDouble(priceRise.getText().toString()) >= Double.parseDouble(setWarnPrice.getText().toString())) {
            df.format(Double.parseDouble(priceRise.getText().toString()));
            bs.append("10,");
            configValue.append(Double.parseDouble(priceRise.getText().toString()));
            configValue.append(",");
        }
        //下跌价格编辑框不为空且输入值小于当前价格
        if (priceFall.getText().toString().length() != 0 && Double.parseDouble(priceFall.getText().toString()) <= Double.parseDouble(setWarnPrice.getText().toString())) {
            df.format(Double.parseDouble(priceFall.getText().toString()));
            bs.append("20,");
            configValue.append(Double.parseDouble(priceFall.getText().toString()));
        }
        //盘中大涨/大跌按钮打开
        if (btnPan.isChecked()) {
            bs.append("30,40,");
            configValue.append(",0.1,0.1");
        }
        //极速拉升/猛烈打压按钮打开
        if (btnJs.isChecked()) {
            bs.append("50,60,");
            configValue.append(",0.01,0.01");
        }
        //极速反弹/高台跳水按钮打开
        if (btnFt.isChecked()) {
            bs.append("70,80");
            configValue.append(",0.05,0.05");
        }

        String configTypeId = bs.toString();
        map.put("configTypeId", configTypeId);
        map.put("configValue", configValue.toString());


        LKUtil.getHttpManager().postBody(HttpConstant.SETWARN_URL, map, setWarnHanlder);
    }

    /**
     * 设置预警网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> setWarnHanlder = new BaseHttpAsycResponceHandler<String>() {

        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);
            //成功后跳转到预警主界面
            setResult(RESULT_OK);
            Toast.makeText(SetWarnActivity.this, "配置成功", Toast.LENGTH_SHORT).show();
            finish();
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
            ToastUtils.custom(message);
        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    @Override
    protected void initData() {
        super.initData();
    }

    /**
     * 获取用户短信数量
     */
    public void getUserShortMessage() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("source", "10");
        LKUtil.getHttpManager().postBody(HttpConstant.GETUSERNUM_URL, map, getUserShortMessageHandler);
    }

    /**
     * 获取用户短信数量的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<UserSmsInfoBean> getUserShortMessageHandler = new BaseHttpAsycResponceHandler<UserSmsInfoBean>() {

        @Override
        public void onSuccess(UserSmsInfoBean userSmsInfoBean) {
            super.onSuccess(userSmsInfoBean);
            //如果短信条数为0 短信提醒按钮为关闭状态 弹出短信为0的弹框
            if (userSmsInfoBean.getSmsNum().equals("0")) {
                dialogShowShortMessage();
                btnShortMessage.setChecked(false);
            }
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    /**
     * 获取用户语音数量
     */
    public void getUserVoice() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("source", "10");
        LKUtil.getHttpManager().postBody(HttpConstant.GETUSERNUM_URL, map, getUserVoiceHandler);
    }

    /**
     * 检测用户是否关注公众号
     */
    public void getUserWeiXin() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        LKUtil.getHttpManager().postBody(HttpConstant.CHECK_WECHAT_URL, map, getUserWeiXinHandler);
    }

    /**
     * 检测是否关注公众号的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> getUserWeiXinHandler = new BaseHttpAsycResponceHandler<String>() {

        @Override
        public void onSuccess(String userSmsInfoBean) {
            super.onSuccess(userSmsInfoBean);
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
            //请求失败 代表未关注公众号 弹出关注公众号的弹框 微信提醒按钮状态为关闭
            dialogShowGZH();
            btnWeiXin.setChecked(false);
        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    /**
     * 获取用户语音条数的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<UserSmsInfoBean> getUserVoiceHandler = new BaseHttpAsycResponceHandler<UserSmsInfoBean>() {

        @Override
        public void onSuccess(UserSmsInfoBean userSmsInfoBean) {
            super.onSuccess(userSmsInfoBean);
            //如果为0 弹出语音条数为0的弹框 语音提醒按钮状态为关闭
            if (userSmsInfoBean.getVoiceNum().equals("0")) {
                dialogShowVoice();
                btnTel.setChecked(false);
            }
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    /**
     * 用户短信为0 弹出的对话框
     */
    private void dialogShowShortMessage() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.activity_shortmessagezero, null);
        //builer.setView(v);//这里如果使用builer.setView(v)，自定义布局只会覆盖title和button之间的那部分
        final Dialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);//自定义布局应该在这里添加，要在dialog.show()的后面
        //dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
    }

    /**
     * 用户语音为0 弹出的对话框
     */
    private void dialogShowVoice() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.activity_voicezero, null);
        //builer.setView(v);//这里如果使用builer.setView(v)，自定义布局只会覆盖title和button之间的那部分
        final Dialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);//自定义布局应该在这里添加，要在dialog.show()的后面
        //dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
    }

    /**
     * 关注公众号的对话框
     */
    private void dialogShowGZH() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.activity_gzgzh, null);
        //builer.setView(v);//这里如果使用builer.setView(v)，自定义布局只会覆盖title和button之间的那部分
        final Dialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);//自定义布局应该在这里添加，要在dialog.show()的后面
        //dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initEvent() {
        super.initEvent();

        //返回
        btbvSetWarn.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                finish();
            }

            @Override
            public void leftCloseClick() {

            }
            @Override
            public void rightOnClick() {

            }
        });
//
//        //自定义设置内容隐藏显示
//        setCustom.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (customContent.getVisibility() == View.VISIBLE) {
//                    customContent.setVisibility(View.GONE);
//                    customIcon.setImageResource(R.mipmap.jiantouxia);
//                } else {
//                    customContent.setVisibility(View.VISIBLE);
//                    customIcon.setImageResource(R.mipmap.dakai_icon);
//                }
//            }
//        });

//        //预警设置内容隐藏显示
//        setJs.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//
//                if (jsContent.getVisibility() == View.VISIBLE) {
//                    jsContent.setVisibility(View.GONE);
//                    jsIcon.setImageResource(R.mipmap.jiantouxia);
//                } else {
//                    jsContent.setVisibility(View.VISIBLE);
//                    jsIcon.setImageResource(R.mipmap.dakai_icon);
//                }
//
//                setJs.setFocusable(true);
//                setJs.setFocusableInTouchMode(true);
//                setJs.requestFocus();
//
//                return false;
//            }
//        });
//
//        //预警方式内容隐藏显示
//        setWay.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//
//                if (wayContent.getVisibility() == View.VISIBLE) {
//                    wayContent.setVisibility(View.GONE);
//                    wayIcon.setImageResource(R.mipmap.jiantouxia);
//                } else {
//                    wayContent.setVisibility(View.VISIBLE);
//                    wayIcon.setImageResource(R.mipmap.dakai_icon);
//                }
//
//                setWay.setFocusable(true);
//                setWay.setFocusableInTouchMode(true);
//                setWay.requestFocus();
//                return false;
//            }
//        });

        //保存设置点击事件
        //提醒设置选一项 上涨价格不小于当前价格 下跌价格不大于当前价格方可保存设置
        btnSave.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {


                boolean isFinish = false;
                boolean isXdMatch = true;
                boolean isSzMatch = true;
                if (priceFall.getText().toString().length() > 0) {
                    isFinish = true;

                    if (Double.parseDouble(priceFall.getText().toString()) > Double.parseDouble(setWarnPrice.getText().toString())){
                        isXdMatch = false;
                    }
                }
                if (priceRise.getText().toString().length() > 0) {
                    isFinish = true;

                    if (Double.parseDouble(priceRise.getText().toString()) < Double.parseDouble(setWarnPrice.getText().toString())){

                        isSzMatch = false;
                    }
                }
                if (btnPan.isChecked()) {
                    isFinish = true;
                }
                if (btnJs.isChecked()) {
                    isFinish = true;
                }
                if (btnFt.isChecked()) {
                    isFinish = true;
                }

                if (isFinish && isSzMatch && isXdMatch) {
                    setWarnConfig();
                }else if (priceRise.getText().toString().length() > 0){

                    if (Double.parseDouble(priceRise.getText().toString()) < Double.parseDouble(setWarnPrice.getText().toString())){
                    Toast.makeText(SetWarnActivity.this, "上涨价格不能小于当前价格", Toast.LENGTH_SHORT).show();
                    }else if (priceFall.getText().toString().length() > 0){
                        if (Double.parseDouble(priceFall.getText().toString()) > Double.parseDouble(setWarnPrice.getText().toString())){
                            Toast.makeText(SetWarnActivity.this, "下跌价格不能大于当前价格", Toast.LENGTH_SHORT).show();}
                    }
                }else if (priceFall.getText().toString().length() > 0){
                    if (Double.parseDouble(priceFall.getText().toString()) > Double.parseDouble(setWarnPrice.getText().toString())){
                    Toast.makeText(SetWarnActivity.this, "下跌价格不能大于当前价格", Toast.LENGTH_SHORT).show();
                    }else if (priceRise.getText().toString().length() > 0){
                        if (Double.parseDouble(priceRise.getText().toString()) < Double.parseDouble(setWarnPrice.getText().toString())){
                            Toast.makeText(SetWarnActivity.this, "上涨价格不能小于当前价格", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                else {
                    ToastUtils.custom("必须选择一种预警方式进行保存");
                }
            }
        });

        //计算上涨价格
        percentageRise.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    if (!StringUtil.isEmpty(percentageRise.getText().toString())) {
                        setXD(1, Double.parseDouble(percentageRise.getText().toString()));
                    }
                }
            }
        });

        //计算下跌价格
        percentageFall.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    if (!StringUtil.isEmpty(percentageFall.getText().toString())) {
                        setXD(3, Double.parseDouble(percentageFall.getText().toString()));
                    }
                }
            }
        });

        //计算上涨百分比
        priceRise.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    if (!StringUtil.isEmpty(priceRise.getText().toString())) {
                        setXD(2, Double.parseDouble(priceRise.getText().toString()));
                    }
                }
            }
        });

        //计算下跌百分比
        priceFall.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    if (!StringUtil.isEmpty(priceFall.getText().toString())) {
                        setXD(4, Double.parseDouble(priceFall.getText().toString()));
                    }
                }
            }
        });

        //语音按钮
        btnTel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b && !isOpen) {
                    getUserVoice();
                }
            }
        });

        //公众号按钮
        btnWeiXin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b && !isOpen) {
                    getUserWeiXin();
                }
            }
        });

        //短信按钮
        btnShortMessage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b && !isOpen) {
                    getUserShortMessage();
                }
            }
        });

        //获取焦点
        reLostFocus.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                reLostFocus.setFocusable(true);
                reLostFocus.setFocusableInTouchMode(true);
                reLostFocus.requestFocus();

                return false;
            }
        });

        //获取焦点
        txtLostFocus.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                txtLostFocus.setFocusable(true);
                txtLostFocus.setFocusableInTouchMode(true);
                txtLostFocus.requestFocus();

                return false;
            }
        });

    }

    //手机返回键刷新返回页面
    @Override
    public void onBackPressed() {
        super.onBackPressed();//注销该方法，相当于重写父类这个方法
        finish();
    }
}
